<template>
    <div>
        <!-- 搜索栏 -->
        <el-card class="!border-none mb-4" shadow="never">
            <el-form class="mb-[-16px]" :model="queryParams" :inline="true">
                <el-form-item label="来源用户">
                    <div class="w-[250px]">
                        <el-input
                            v-model="queryParams.user"
                            placeholder="请输入用户编号"
                            clearable
                            @keyup.enter="resetPaging"
                        />
                    </div>
                </el-form-item>
                <el-form-item label="来源文件">
                    <div class="w-[250px]">
                        <el-input
                            v-model="queryParams.filename"
                            placeholder="请输入文档名称"
                            clearable
                            @keyup.enter="resetPaging"
                        />
                    </div>
                </el-form-item>
                <el-form-item label="创建时间">
                    <date-picker
                        v-model:startTime="queryParams.start_time"
                        v-model:endTime="queryParams.end_time"
                    />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPaging">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>

        <!-- 表格栏 -->
        <el-card v-loading="pager.loading" class="!border-none" shadow="never">
            <el-table :data="pager.lists" size="large" class="mt-4">
                <el-table-column label="ID" prop="id" width="100" />
                <el-table-column label="来源用户" prop="user" width="200">
                    <template #default="{ row }">
                        <div class="flex items-center">
                            <el-avatar :size="38" :src="row?.user?.avatar"/>
                            <div class="ml-2">
                                <div class="text-sm">{{ row?.user?.nickname }}</div>
                                <div class="text-xs text-tx-placeholder">{{ row?.user?.sn }}</div>
                            </div>
                        </div>
                    </template>
                </el-table-column>

                <el-table-column label="来源文件" min-width="100">
                    <template #default="{ row }">
                        <div v-if="row.filename" class="flex items-center">
                            <el-image
                                :src="suffixUtil.findUnitIcon(row.filename)"
                                class="w-[34px] h-[38px]"
                            />
                            <p class="ml-1.5">{{ row?.filename  }}</p>
                        </div>
                        <div v-else>-</div>
                    </template>
                </el-table-column>
                <el-table-column label="提问问题" prop="question" min-width="100" >
                    <template #default="{ row }">
                        <p class="line-clamp-2">{{ row?.question || '-' }}</p>
                    </template>
                </el-table-column>
                <el-table-column label="记录场景" prop="scene" width="100">
                    <template #default="{ row }">
                        <el-tag type="info" v-if="row.scene === 'abstract'">摘要总结</el-tag>
                        <el-tag type="info" v-if="row.scene === 'translate'">全文翻译</el-tag>
                        <el-tag type="info" v-if="row.scene === 'dialogue'">提问对话</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="消耗积分" prop="use_points" width="100" />
                <el-table-column label="消耗Tokens" prop="use_tokens" width="100" />
                <el-table-column label="来源IP" prop="ip" width="130" />
                <el-table-column label="提问时间" prop="create_time" width="180" />
                <el-table-column label="操作" width="150" fixed="right">
                    <template #default="{ row }">
                        <el-button
                            type="primary"
                            link
                            @click="handleDetail(row)"
                        >
                            查看回复
                        </el-button>
                        <el-button
                            v-perms="['apply:docs:record:delete']"
                            type="danger"
                            link
                            @click="handleDelete(row.id)"
                        >
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <paging v-model="pager" @change="queryLists" />
            </div>
        </el-card>

        <popup
            :show="detailShow"
            title="详细信息"
            width="700px"
            confirmButtonText=""
            cancelButtonText=""
            @close="detailShow = false"
        >
            <div class="p-6">
                <div class="pb-3">记录编号：{{ detailInfo.code }}</div>
                <div class="pb-3">记录时间：{{ detailInfo?.create_time || '-' }}</div>
                <div class="pb-3">来源的IP：{{ detailInfo?.ip || '-' }}</div>
                <div class="pb-6">来源文件：{{ detailInfo.filename || '-' }}</div>

                <div class="mb-1 text-lg font-bold">提问问题</div>
                <div class="p-3 mb-4 bg-page">
                    {{ detailInfo?.question || '-' }}
                </div>

                <div class="mb-1 text-lg font-bold">回复内容：</div>
                <div class="p-3 bg-page">
                    {{ detailInfo?.answer }}
                </div>
            </div>
        </popup>
    </div>
</template>

<script setup lang="ts">
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import suffixUtil from '@/utils/suffix'
import docsRecordApi from '@/api/apply/docs/record'

interface DetailTs {
    code: string
    filename: string
    question: string
    answer: string
    ip: string
    create_time: string
}

// 详情信息
const detailShow = ref<boolean>(false)
const detailInfo = ref<DetailTs>({} as DetailTs)

// 查询参数
const queryParams = reactive({
    user: '',
    filename: '',
    start_time: '',
    end_time: '',
    is_free: ''
})

// 分页查询
const { pager, queryLists, resetParams, resetPaging } = usePaging({
    fetchFun: docsRecordApi.lists,
    params: queryParams
})

/**
 * 处理详情
 *
 * @param row
 */
const handleDetail = (row: any) => {
    detailShow.value = true
    detailInfo.value = row
}

/**
 * 处理删除
 *
 * @param {number} id
 * @returns {Promise<void>}
 */
const handleDelete = async (id: number): Promise<void> => {
    feedback.confirm('确定要删除此项数据吗?')
        .then(async () => {
            await docsRecordApi.delete(id)
            feedback.msgSuccess('删除成功')
            await queryLists()
        }).catch(() => {})
}

onMounted(async () => {
    await queryLists()
})
</script>
